<template>
	<el-dialog title="新增外部链接" append-to-body :visible.sync="visible" width="560px">
		<el-form size="medium" ref="form" :model="formData" :rules="formRules" label-width="110px" class="link_form_wrapper">
			<el-form-item label="外部链接名称" prop="oldName">
				<el-input v-model="formData.oldName"></el-input>
			</el-form-item>
			<el-form-item label="外部链接地址" prop="attrAddr">
				<el-input v-model="formData.attrAddr"></el-input>
			</el-form-item>
			<el-row>
				<el-col :span="12">
					<el-form-item label="文件类型" prop="showType">
						<sw-dict-select v-model="formData.showType" dict-name="FILE_TYPE" placeholder="请选择文件类型"></sw-dict-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="排序" prop="sort">
						<el-input-number v-model="formData.sort" controls-position="right" :min="1"></el-input-number>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="外部链接描述" prop="content">
				<el-input type="textarea" :autosize="{ minRows: 3, maxRows: 6 }" v-model="formData.content"></el-input>
			</el-form-item>
		</el-form>
		<template slot="footer">
			<el-button size="medium" @click="visible = false">取消</el-button>
			<el-button size="medium" type="primary" :loading="btnLoading" @click="handleSubmitForm">确定</el-button>
		</template>
	</el-dialog>
</template>

<script>
import { mapGetters } from 'vuex';
import { addedFileLink } from '@/api/data/tbNetDisk';
export default {
	props: {
		value: Boolean,
		data: {
			type: Object,
			default: () => {}
		}
	},
	data() {
		const linkAddr = (rule, value, callback) => {
			let reg = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/;
			if (!value) {
				callback(new Error('请输入外部链接地址'));
			} else if (!reg.test(value)) {
				callback(new Error('请输入正确的外部链接地址'));
			} else {
				callback();
			}
		};
		return {
			visible: this.value,
			formData: {},
			formRules: {
				oldName: [{ required: true, trigger: 'blur', message: '请输入外部链接名称' }],
				attrAddr: [{ required: true, validator: linkAddr, trigger: 'blur' }],
				showType: [{ required: true, trigger: 'change', message: '请选择文件类型' }]
			},
			btnLoading: false
		};
	},
	computed: {
		...mapGetters(['dictEnums']),
		addOrEdit() {
			return Object.keys(this.data).length === 0;
		},
		libraryId() {
			return this.$route.params.id;
		},
		checkedFolder() {
			return this.$route.query.folder || '0';
		}
	},
	watch: {
		value(flag) {
			this.visible = flag;
		},
		visible(flag) {
			this.$emit('input', flag);
			if (!flag) return false;
			this.$nextTick(() => {
				this.$refs['form'].resetFields();
				this.formData = this.addOrEdit ? {} : { ...this.data };
			});
		}
	},
	methods: {
		//提交外部链接
		handleSubmitForm() {
			this.$refs['form'].validate(async (valid) => {
				if (!valid) return false;
				this.btnLoading = true;
				await this.addedFileLinkRequest();
				this.btnLoading = false;
			});
		},

		//添加外部链接请求
		async addedFileLinkRequest() {
			let params = { ...this.formData, dirId: this.libraryId, pid: this.checkedFolder, isSubmit: '1', systemKey: '1' };
			let result = await addedFileLink(params).then((res) => res.data);
			if (result.code === '0') {
				this.visible = false;
				this.$message.success('添加外部链接成功');
				this.$emit('refresh');
			} else {
				this.$message.error('添加外部链接失败');
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.link_form_wrapper {
	width: calc(100% - 10px);
	padding: 10px 10px 0 0;
	.is-controls-right {
		width: 100%;
	}
}
</style>
